<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Modeladmin extends CI_Model{
    var $usr;
    var $pwd;
    var $usr_data;  

    public function __construct(){
        $this->usr = '';
        $this->pwd = '';
    }

    public function set_user($user){
        $this->usr = $user;
        $this->usr_data['c_usr'] = $user;
    }

    public function set_password($password){
        $this->pwd = $password;
        $this->usr_data['c_pwd'] = md5($password);
    }

    public function do_login(){

        $query = "Select * from member where username='$this->usr' and password='$this->pwd'";
        $result = $this->db->query($query);
        $rows = $result->row_array();

        if ($this->db->affected_rows() > 0) {
            $this->usr_data['c_telephone'] = $rows['telephone'];
            $this->usr_data['c_fullname'] = $rows['fullname'];
            $this->usr_data['c_email'] = $rows['email'];
            $this->usr_data['c_lastlog']  = $rows['lastlog'];
            $this->usr_data['c_id']       = $rows['idmember'];

            $this->set_cookie();
            $this->_update_log($rows['idmember']);

            return true;
        } else {
            return false;
        }

    }   

    public function do_logout(){
        $sess = array();

        $sess['c_usr'] = '';
        $sess['c_pwd'] = '';
        $sess['c_id']  = '';
        $sess['c_lastlog'] = '';
        $sess['c_fullname'] = '';
        $sess['c_email'] = '';
        $sess['c_telephone'] = '';

        $this->session->unset_userdata($sess);
        $this->session->sess_destroy();
    }

    public function ubah_member($data){
        $this->db->where('idadmin',$data['idmember']);
        $ubah=$this->db->update('member', $data);
        return $ubah;
    }

    public function get_member($id){
        $this->db->where('idmember', $id);
        $query = $this->db->get('member');

        return $query->row_array();
    }

    protected function set_cookie(){
        $this->session->set_userdata($this->usr_data);
    }

    public function get_cookie(){
        $sess = array();

        $sess['c_usr'] = $this->session->userdata('c_usr');
        $sess['c_pwd'] = $this->session->userdata('c_pwd');
        $sess['c_fullname'] = $this->session->userdata('c_fullname');
        $sess['c_email'] = $this->session->userdata('c_email');
        $sess['c_lastlog']  = $this->session->userdata('c_lastlog');
        $sess['c_telephone']  = $this->session->userdata('c_telephone');
        $sess['c_id']  = $this->session->userdata('c_id');

        return $sess;
    }

    public function validate_cookie(){
        if ($this->session->userdata('c_usr') != '' && $this->session->userdata('c_pwd') != '')
            return true;
        else
            return false;
    }

    private function _update_log($id) {
        $str = "update member set lastlog=now() where idmember=$id";
        $query = $this->db->query($str);

        if ($this->db->affected_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }

    //ini uji coba
	public function bacadata(){
        $baca = $this->db->get('kategori');
        if($baca->num_rows() > 0){
            foreach ($baca->result() as $data){
                $hasil[] = $data;
            }
            return $hasil;
        }
    }

    public function getKategoriProduk(){
        $query = $this->db->query("SELECT * FROM kategori ORDER BY nama_kategori ASC");
        return $query;
    }

    public function get_data_kategori(){
        $baca=$this->db->get('kategori');
        $data=$baca->result();
        return $data;
    }
    
    public function get_data_kategori_by_id($data){
        $this->db->where($data);
        $baca=$this->db->get('kategori');
        
        $data=$baca->first_row();
        return $data;
    }

    public function tambah_kategori($data){
        $baca=$this->db->insert('kategori',$data);
        return $baca;
    }

    public function ubah_kategori($data){
        $this->db->where('id_kategori',$data['id_kategori']);
        $ubah=$this->db->update('kategori', $data);
        return $ubah;
    }

    public function hapus_kategori($id_kategori)
    {
        $query = $this->db->query("DELETE FROM kategori WHERE id_kategori = '$id_kategori'");
    }

    //Model Banner slider
    public function get_data_banner(){
        $baca = $this->db->get('banner');
        if($baca->num_rows() > 0){
            foreach ($baca->result() as $data){
                $hasil[] = $data;
            }
            return $hasil;
        }
    }
    
    public function hapusbanner($id){
        $this->db->query("DELETE FROM banner WHERE id = '$id'");
    }
    
    public function getBannerById($id){
        $query = $this->db->query("SELECT * FROM banner WHERE id = '$id'");
        return $query;
    }
    
    public function updateStatusBanner($id, $status){
        $query = $this->db->query
        ("UPDATE banner SET status = '$status', created_at = NOW()  WHERE id = '$id'");
    }

    public function tambah_banner($foto){
        $this->db->query("INSERT INTO banner VALUES('', '$foto', '0', NOW() )");
    }

}
